/**
 * Copyright 2013 Bhushan
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.s4j.signals.helper;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

import org.s4j.signals.core.Invocable;
import org.s4j.signals.core.SignalContainer;
import org.s4j.signals.core.SlotContainer;
import org.s4j.signals.exception.InvalidConnectionException;

/**
 * Utility methods to help validate that connections can be made across different combinations of invocable objects
 * 
 * @author Bhushan
 * 
 */
public class Validator {

    /**
     * Validates to see if a connection is possible between the requested source and destination.
     * 
     * @throws InvalidConnectionException
     *             in case signatures are incompatible
     */
    @SuppressWarnings("rawtypes")
    public static void validateConnection(final SignalContainer source, final SignalContainer destination)
            throws InvalidConnectionException {

        final Class[] sourceParams = source.getParameterTypes();
        final Class[] destParams = destination.getParameterTypes();

        if (sourceParams.length != destParams.length) {
            throw new InvalidConnectionException("Parameters mismatch");
        }

        int length = sourceParams.length;
        for (int index = 0; index < length; index++) {
            if (!sourceParams[index].equals(destParams[index])) {
                throw new InvalidConnectionException("Parameter types mismatch");
            }
        }
    }

    /**
     * Validates to see if a connection is possible between the requested source and destination.
     * 
     * @throws InvalidConnectionException
     *             in case the signatures are not compatible
     */
    @SuppressWarnings("rawtypes")
    public static void validateConnection(final SignalContainer source, final SlotContainer destination)
            throws InvalidConnectionException {

        final Class[] sourceParams = source.getParameterTypes();
        final Method destMethod = destination.getMethod();
        final Type[] destParams = destMethod.getGenericParameterTypes();

        if (sourceParams.length != destParams.length) {
            throw new InvalidConnectionException("Parameters mismatch");
        }

        int length = sourceParams.length;
        for (int index = 0; index < length; index++) {
            if (!sourceParams[index].equals(destParams[index])) {
                throw new InvalidConnectionException("Parameter types mismatch");
            }
        }
    }

    /**
     * Validates to see if a connection is possible between the requested source and destination.
     * 
     * @throws InvalidConnectionException
     *             in case te signatures are not compatible
     */
    public static void validateConnection(final SlotContainer source, final Invocable destination)
            throws InvalidConnectionException {
        throw new InvalidConnectionException("Slot cannot be connected to another slot/signal");
    }
}
